<?php

namespace Api\Model;
class ArticleModel extends CommonModel{

	public function info($article_id){
		$where['id'] = $article_id;
		$info = $this->where($where)->find();
		$info = $this->filter_field($info);
		return $info;
	}
	public function filter_field($data){
		$status_desc = array(0 => '禁用', 1 => '通过', 2 => '待审核', 3 => '不通过');
		$data['article_id']      = $data['id'];
		$data['date_desc']       = date('Y-m-d', $data['edit_time']);
		$data['time_desc']       = time_ago_desc($data['edit_time']);
		$data['datetime_desc']   = date('Y-m-d H:i:s', $data['edit_time']);
		$data['cover_url']       = D('File')->get_path($data['cover_id']);
		$data['cover_file_info'] = D('File')->get_file_list($data['cover_id']);
		$data['category_name']   = D('Category')->get_name($data['category_id']);
		$data['channel_name']    = $this->get_field_val('Channel', $data['channel_id']);
		$data['link_url']        = $this->get_short_link_url($data['link_id']);
		$data['user_name']       = D('User')->get_user_name($data['user_id']);
		$data['user_head']       = D('User')->get_user_head_url($data['user_id']);
		$data['status_desc']     = $status_desc[$data['status']];
		$data['content']         = $this->get_content($data['article_id']);

		unset($data['id']);
		return $data;
	}
	public function update_num($article_id = '', $field = 'fav_num', $type = 'add'){
		if(!$article_id){
			return false;
		}
		$where['id'] = $article_id;
		if($type == 'add'){
			$this->where($where)->setInc($field);
		}
		if($type == 'minus'){
			$this->where($where)->setDec($field);
		}
	}
	/**
	 * 获取文章内容
	 * @param  string $article_id 文章ID
	 */
	public function get_content($article_id = ''){
		$where['article_id'] = $article_id;
		$content = D('ArticleContent')->where($where)->getField('content');
		return $content ? $content : '';
	}
	/**
	 * 更新查看次数
	 * @param  [type] $article_id 文章ID
	 * @param  [type] $user_id    用户ID
	 */
	public function update_view_num($article_id, $user_id){
		if(!$article_id || !$user_id){
			return false;
		}

		$where['user_id']    = $user_id;
		$where['article_id'] = $article_id;
		if(D('ArticleVisitLogs')->where($where)->count()){
			return false;
		}

		$data['user_id']     = $user_id;
		$data['article_id']  = $article_id;
		$data['ip']          = get_client_ip();
		$data['create_time'] = time();
		D('ArticleVisitLogs')->add($data);

		unset($where);
		$where['id'] = $article_id;
		D('Article')->where($where)->setInc('view_num');

		return true;
	}
	/**
	 * 记录文章获取金额
	 * @param  [type] $user_id    用户ID
	 * @param  [type] $article_id 文章ID
	 * @param  [type] $money      金额
	 * @param  string $get_type   获取类型
	 */
	public function record_get_money($user_id, $article_id, $money, $get_type = 'read_article'){
		$where['id'] = $article_id;
		$article_user_id = $this->where($where)->getField('user_id');

		$data['user_id']         = $user_id;
		$data['article_user_id'] = $article_user_id ? $article_user_id : 0;
		$data['article_id']      = $article_id;
		$data['money']           = $money;
		$data['get_type']        = $get_type;
		$data['app_type']        = $_SERVER['HTTP_APP_TYPE'] ? $_SERVER['HTTP_APP_TYPE'] : '';
		$data['ip']              = get_client_ip();
		$data['create_time']     = time();

		D('ArticleGetMoney')->add($data);
	}
	/**
	 * 判断用户是否获取到金额
	 * @param  [type]  $user_id    用户ID
	 * @param  [type]  $article_id 文章ID
	 * @param  [type]  $get_type   获取类型
	 */
	public function is_get_money($user_id, $article_id, $get_type){
		if(!$user_id || !$article_id || !$get_type){
			return false;
		}
		$where['user_id']    = $user_id;
		$where['article_id'] = $article_id;
		$where['get_type']   = $get_type;
		$count = D('ArticleGetMoney')->where($where)->count();

		return $count > 0;
	}
}